/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author user
 */
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

//package opis_mkpb;

/**
 *
 * @author user
 */
import com.csvreader.CsvReader;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
/*
* CSVReaderDemo.java
*
* Created on May 22, 2007, 11:20 AM
*
*/

/**
*
* @author BEN
*/
public class read_csv_to_jtable extends JPanel{
private String filePath = "F:/NetBeansProjects/STAT_GUI/out/DATA.txt";

/** Creates a new instance of CSVReaderDemo */
public read_csv_to_jtable() {
super(new GridLayout(1,0));
int headerCount,col,row=0;
try {
    //initialized a CsvReader object with file path and delimiter
    CsvReader reader = new CsvReader(filePath,',');

    // you have to always calll readHeaders first before you do any other operation
    reader.readHeaders();
    //Get CSV file header information
    String[] headers = reader.getHeaders();
    //Get the count of headers
    headerCount = reader.getHeaderCount();
    col = reader.getColumnCount();
    List<Object[]> list = new ArrayList<Object[]>();
    while(reader.readRecord())
        {
        //add each row to the list
        list.add(reader.getValues());
        }
    Object[][] data = new String[list.size()][];
    for(int i=0; i<list.size(); i++)
    data[i] = list.get(i);

    final JTable table = new JTable(data, headers);
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    table.setFillsViewportHeight(true);
    //Create the scroll pane and add the table to it.
    JScrollPane scrollPane = new JScrollPane(table);

    //Add the scroll pane to this panel.
    add(scrollPane);
} catch (Exception ex) {
ex.printStackTrace();
}
}

/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI(){
//Create and set up the window.
JFrame frame = new JFrame("SimpleTableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.
read_csv_to_jtable newContentPane = new read_csv_to_jtable();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);

//Display the window.
frame.pack();
frame.setVisible(true);
}


public static void main(String[] args){
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}